package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AppmanagerMapper;
import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.service.IAppmanagerService;

import javax.annotation.Resource;

/**
 * 项目信息Service业务层处理
 * 
 * @author yaozh
 * @date 2024-01-09
 */
@Service
public class AppmanagerServiceImpl implements IAppmanagerService 
{
    @Value("${centralDatabase.dbName}")
    private String centralDatabase;

    @Resource
    private AppmanagerMapper appmanagerMapper;
    @Resource
    private ISysDeptService deptService;

    /**
     * 查询项目信息
     * 
     * @param appid 项目信息主键
     * @return 项目信息
     */
    @Override
    public Appmanager selectAppmanagerByAppid(Long appid)
    {
        return appmanagerMapper.selectAppmanagerByAppid(appid);
    }

    /**
     * 查询项目信息列表
     * 
     * @param appmanager 项目信息
     * @return 项目信息
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Appmanager> selectAppmanagerList(Appmanager appmanager)
    {
        return appmanagerMapper.selectAppmanagerList(appmanager);
    }

    /**
     * 根据用户查询项目信息列表
     *
     * @param user 用户
     * @return 项目信息
     */
    @Override
    public List<Appmanager> selectAppManagerListByUserAndDbName(LoginUser user,String dbName)
    {
        List<Appmanager> appList = selectAppManagerListByUser(user);
        if(appList == null || appList.size() == 0 || !appList.stream().map(appManager -> {return (appManager.getAppid() + appManager.getAppName());}).collect(Collectors.toList()).contains(dbName)){
            return new ArrayList<>();
        }
        List<Appmanager> newList = new ArrayList<>();
        for(Appmanager appManager : appList){
            String tempDbName = appManager.getAppid() + appManager.getAppName();
            if(org.springframework.util.StringUtils.hasLength(tempDbName) && tempDbName.equals(dbName)){
                newList.add(appManager);
            }
        }
        return newList;
    }

    @Override
    public List<Appmanager> selectAppManagerListByUser(LoginUser user)
    {
        List<SysDept> list = deptService.selectDeptList(new SysDept());//会自动根据数据权限返回部门列表
        List<Long> sysDeptIds = new ArrayList<>();
        if(list != null && list.size() > 0){
            list.stream().forEach(sysDept -> {
                sysDeptIds.add(sysDept.getDeptId());
            });
        }
        if(sysDeptIds.size() == 0){
            return new ArrayList<>();
        }
        return appmanagerMapper.selectAppManagerListByDeptIds(sysDeptIds.toArray(new Long[0]),user.getUserId());
    }

    /**
     * 新增项目信息
     * 
     * @param appmanager 项目信息
     * @return 结果
     */
    @Override
    public int insertAppmanager(Appmanager appmanager)
    {
        //新增
        appmanager.setAppstate("0");//未初始化
        appmanager.setState("0");//正常。未被删除
        appmanager.setRunStatus(0L);//运行状态

        appmanager.setCreateTime(DateUtils.getNowDate());
        return appmanagerMapper.insertAppmanager(appmanager);
    }

    /**
     * 修改项目信息
     * 
     * @param appmanager 项目信息
     * @return 结果
     */
    @Override
    public int updateAppmanager(Appmanager appmanager)
    {
        return appmanagerMapper.updateAppmanager(appmanager);
    }

    /**
     * 批量删除项目信息
     * 
     * @param appids 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    public int deleteAppmanagerByAppids(Long[] appids)
    {
        //return appmanagerMapper.deleteAppmanagerByAppids(appids);
        return 0;
    }

    /**
     * 批量更新项目状态信息
     *
     * @param appIds 需要更新状态的项目信息主键
     * @return 结果
     */
    @Override
    public int updateAppManagerStatusByAppIds(Long[] appIds,String state,String runStatus)
    {
        return appmanagerMapper.updateAppManagerStatusByAppIds(appIds,state,runStatus);
    }

    /**
     * 删除项目信息信息
     * 
     * @param appid 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteAppmanagerByAppid(Long appid)
    {
        //return appmanagerMapper.deleteAppmanagerByAppid(appid);
        return 0;
    }

    /**
     * 更新项目状态信息
     *
     * @param appId 需要更新状态的项目信息主键
     * @return 结果
     */
    @Override
    public int updateAppManagerStatusByAppId(Long appId,String state,String runStatus)
    {
        return appmanagerMapper.updateAppManagerStatusByAppId(appId,state,runStatus);
    }

    @Override
    public boolean checkAppExplainUnique(Appmanager appmanager) {
        Long appId = StringUtils.isNull(appmanager.getAppid()) ? -1L : appmanager.getAppid();
        Appmanager info = appmanagerMapper.checkAppExplainUnique(appmanager.getAppexplain());
        if (StringUtils.isNotNull(info) && info.getAppid().longValue() != appId.longValue())
        {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkAppNameUnique(Appmanager appmanager) {
        Long appId = StringUtils.isNull(appmanager.getAppid()) ? -1L : appmanager.getAppid();
        Appmanager info = appmanagerMapper.checkAppNameUnique(appmanager.getAppName());
        if (StringUtils.isNotNull(info) && info.getAppid().longValue() != appId.longValue())
        {
            return false;
        }
        return true;
    }

    @Override
    public int initAppManager(Appmanager appmanager){
        try{
            String dbName = appmanager.getAppid() + appmanager.getAppName();
            appmanagerMapper.createDatabase(centralDatabase,dbName);

            //创建数据库后，更新初始化状态
            appmanager.setAppstate("1");
            return appmanagerMapper.updateAppmanager(appmanager);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int checkDataBaseUnique(String dbName){
        return appmanagerMapper.checkDataBaseUnique(dbName);
    }

    @Override
    public int updateAppUserGroup(Long userId, Long appId, Integer control){
        return appmanagerMapper.updateAppUserGroup(userId, appId, control);
    }

    @Override
    public int insertAppUserGroup(Long userId, Long appId, Integer control){
        return appmanagerMapper.insertAppUserGroup(userId, appId, control);
    }

    @Override
    public int selectAppUserGroupByAppId(Long userId, Long appId){
        return appmanagerMapper.selectAppUserGroupByAppId(userId, appId);
    }

    @Override
    public int deleteAppUserGroupByAppId(Long[] userIds){
        return appmanagerMapper.deleteAppUserGroupByAppId(userIds);
    }
}
